package com.leo.util;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
	
	private final Pattern HTTPURL = Pattern.compile("^(http(s)?://)?([\\w_]+\\.)?[\\w_]+\\.[\\w_]+(:d+)?(/[\\w_]+)*(/([\\w_]+.[\\w_]+)?)?$"),
			EMAIL = Pattern.compile("^[\\w_]+@[\\w_]+\\.[\\w_]+$");
	
	/**
	 * 判断url是否为一个HttpURL
	 * 不包括localhost或者ip地址
	 * 
	 * @param url
	 * @return
	 */
	public boolean isHttpURL(String url){
		Matcher mt = HTTPURL.matcher(url);
		//return mt.matches() ;
		return mt.matches() || url.startsWith("localhost") || url.startsWith("http://localhost");
	}
	
	/**
	 * 判断url是否为email地址
	 * 
	 * @param url
	 * @return
	 */
	public boolean isEmail(String url){
		Matcher mt = EMAIL.matcher("http://www.baidu.com/user/user_login.html");
		return mt.matches();
	}
	
	/**
	 * 判断用户名是否合法
	 * 规则：字母开头，允许字母、数字和"-"、"_"等字符，长度为minLenght到maxLenght之间
	 * 
	 * @param name
	 * @param minLenght
	 * @param maxLenght
	 * @return
	 */
	public boolean userNameLegal(String username, int minLenght, int maxLenght){
		return Pattern.matches(String.format("^[a-zA-Z][\\w-_]{%d,%d}$", minLenght - 1 ,maxLenght - 1), username);
	}

	/**
	 * 判断密码是否合法
	 * 规则：允许字母、数字和"+ - * / . @"等字符，长度为minLenght到maxLenght之间
	 * 
	 * @param name
	 * @param minLenght
	 * @param maxLenght
	 * @return
	 */
	public boolean userPasswordLegal(String password, int minLenght, int maxLenght){
		return Pattern.matches(String.format("^[\\w\\+\\-\\*/@]{%d,%d}$", minLenght, maxLenght), password);
	}
	
	public static void main(String[] args) {
		RegexUtil rg = new RegexUtil();
		while(true){
			Scanner  sc = new Scanner(System.in);
			System.out.println(rg.isHttpURL(sc.nextLine()) );
		}
	}
}
